import pygtk
pygtk.require('2.0')
import gtk
# import os, sy
import os, sys

from mbac.gui.mainpage import MainPage
from mbac.gui.configpage import ConfigPage
from mbac.util.logutil import LogUtil

class MainWindow:
	
	def delete_event(self, widget, event, data=None):
		# If you return FALSE in the "delete_event" signal handler,
		# GTK will emit the "destroy" signal. Returning TRUE means
		# you don't want the window to be destroyed.
		# This is useful for popping up 'are you sure you want to quit?'
		# type dialogs.
		LogUtil.writeLog("delete event occurred")
		
		# Change FALSE to TRUE and the main window will not be destroyed with a "delete_event".
		return False

	def destroy(self, widget, data=None):
		# close log file
		LogUtil.writeLog("destroy signal occurred")
		self.logUtil.closeLogFile()
		print "log file closed"

		gtk.main_quit()
		
	def buildLayout(self, configFile):
		
		# configFile is still not used 

		# from now we write data in logFile
		self.logUtil = LogUtil(None, True)
		
		notebook = gtk.Notebook()
		notebook.set_tab_pos(gtk.POS_TOP)
		notebook.show()
		self.show_tabs = True
		self.show_border = True
		
		# prepare tabs		
		self.tabs = notebook
		self.tabs.set_property("homogeneous",True)
		
		# config page
		configPage = ConfigPage()
		
		# main page
		mainPage = MainPage(configPage)
		
		# append page on notebook
		notebook.append_page(mainPage, gtk.Label("  Game  "))
		notebook.append_page(configPage, gtk.Label("  Config  ")) 
		
		self.window.add(notebook)
		
		
		
		
	def __init__(self, configFile):		
		
		# create a new window
		self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
	
		# When the window is given the "delete_event" signal (this is given by the window manager, usually by the "close" option, or on the titlebar), we ask it to call the delete_event () function as defined above. 
		# The data passed to the callback function is NULL and is ignored in the callback function.
		self.window.connect("delete_event", self.delete_event)
	
		# Here we connect the "destroy" event to a signal handler.  
		# This event occurs when we call gtk_widget_destroy() on the window, or if we return FALSE in the "delete_event" callback.
		self.window.connect("destroy", self.destroy)
	
		# Sets the border width of the window.
		self.window.set_border_width(10)
		
		# title
		self.window.set_title("Mbac")
		
		# size
		self.window.set_default_size(480,640)
		
		self.buildLayout(configFile)
		
		self.window.show()
		
	def main(self):
        
		# All PyGTK applications must have a gtk.main(). Control ends here and waits for an event to occur (like a key press or mouse event).
		gtk.main()
